Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class CategoryGroupBO
    Inherits CategoryGroupDAO


    Public Overloads Shared Function Insert(ByVal obj As CategoryGroup, ByVal iAutoKey As Boolean, ByVal bSave As Boolean) As Boolean
        Dim con As SqlConnection = Nothing
        Dim trans As SqlTransaction = Nothing
        Try
            con = DBHelper.GetConnection()
            trans = con.BeginTransaction()
            If Insert(obj, iAutoKey, con, trans) And bSave Then
                'Update after Inert
            End If
            trans.Commit()
            Return True
        Catch ex As Exception
            If trans IsNot Nothing Then
                trans.Rollback()
            End If
            Throw New Exception("insert CategoryGroup-> " & ex.Message, ex)
        Finally
            If con IsNot Nothing Then
                con.Close()
                con.Dispose()
            End If
            If trans IsNot Nothing Then
                trans.Dispose()
            End If
        End Try
        Return False
    End Function

    Public Overloads Shared Function Update(ByVal obj As CategoryGroup, ByVal bSave As Boolean) As Boolean
        Dim con As SqlConnection = Nothing
        Dim trans As SqlTransaction = Nothing
        Try
            Dim oldObj As CategoryGroup = findByID(obj.ID)
            Dim diff As String = oldObj.compareAndGetDiff(obj)
            If diff <> "" Then
                con = DBHelper.GetConnection()
                trans = con.BeginTransaction()
                If Update(obj, con, trans) And bSave Then
                    'Luu vet
                End If
                trans.Commit()
            End If
            Return True
        Catch ex As Exception
            If trans IsNot Nothing Then
                trans.Rollback()
            End If
            Throw New Exception("error: update Category -> " & ex.Message, ex)
        Finally
            If con IsNot Nothing Then
                con.Close()
                con.Dispose()
            End If
            If trans IsNot Nothing Then
                trans.Dispose()
            End If
        End Try
        Return False
    End Function

    Public Overloads Shared Function DeleteByID(ByVal _ID As String) As Boolean
        Dim con As SqlConnection = Nothing
        Dim trans As SqlTransaction = Nothing
        Try
            con = DBHelper.GetConnection()
            trans = con.BeginTransaction()
            Dim obj As CategoryGroup = findByID(_ID)
            If DeleteByID(_ID) Then
                'Luu vet
            End If
            trans.Commit()
            Return True
        Catch ex As Exception
            If trans IsNot Nothing Then
                trans.Rollback()
            End If
            Throw New Exception("error: delete user -> " & ex.Message, ex)
        Finally
            If con IsNot Nothing Then
                con.Close()
                con.Dispose()
            End If
            If trans IsNot Nothing Then
                trans.Dispose()
            End If
        End Try
        Return False
    End Function
End Class
